const Service = require('./index')
const permissionModel = require('@/models/permission.model')
const staffModel = require('@/models/staff.model')

class PermissionService extends Service {
  async findRoles (name, status) {
    try {
      const data = await permissionModel.findRoles({
        equal: {
          'role.status': status
        },
        like: {
          'role.name': name
        }
      })
      return data
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 角色创建
   * @param {*} attrs
   * @returns
   */
  async createRole (attrs) {
    const { name, status } = attrs
    if (!['0', '1'].includes(status)) {
      return {
        errorMessage: '不合法的status值'
      }
    }
    try {
      await permissionModel.createRole({ name, status })
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 角色基本信息编辑
   * @param {*} attrs
   * @returns
   */
  async updateRole (attrs) {
    const { id, name, status } = attrs
    if (this.isNull(id)) {
      return {
        errorMessage: 'id值不能为空'
      }
    }
    if (!['0', '1'].includes(status)) {
      return {
        errorMessage: '不合法的status值'
      }
    }
    try {
      const exist = await permissionModel.existRole(id)
      if (!exist) {
        return {
          errorMessage: '该角色不存在'
        }
      }
      await permissionModel.updateRole(id, {
        name, status
      })
    } catch (error) {
      throw new Error(error)
    }
  }

  async deleteRole (id) {
    if (this.isNull(id)) {
      return {
        errorMessage: '缺少必要标识id'
      }
    }
    try {
      const role = await permissionModel.findOneRole(id)
      if (!role) {
        return {
          errorMessage: '该角色不存在'
        }
      }
      if (role.type === '0') {
        return {
          errorMessage: '系统角色不允许删除'
        }
      }
      await permissionModel.deleteRole(id)
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 每个角色分配权限
   * @param {*} roleId
   * @param {*} permissionIds
   * @returns
   */
  async roleAllocationPermission (roleId, permissionIds) {
    if (this.isNull(roleId)) {
      return {
        errorMessage: '缺少必要标识roleId'
      }
    }
    try {
      const exist = await permissionModel.existRole(roleId)
      if (!exist) {
        return {
          errorMessage: '该角色不存在'
        }
      }
      await permissionModel.createRolePermission(roleId, permissionIds)
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 查询某个角色下面所有权限资源id
   * @param {*} roleId
   */
  async findRolePermissionIds (roleId) {
    if (this.isNull(roleId)) {
      return {
        errorMessage: '缺少必要标识roleId'
      }
    }
    try {
      const exist = await permissionModel.existRole(roleId)
      if (!exist) {
        return {
          errorMessage: '该角色不存在'
        }
      }
      const data = await permissionModel.findRolePermission(roleId)
      return data.map(o => o.permissionId)
    } catch (error) {
      throw new Error(error)
    }
  }

  /**
   * 每个用员工分配角色
   * @param {*} staffId
   * @param {*} roleIds
   */
  async staffAllocationRole (staffId, roleIds) {
    try {
      const existStaff = await staffModel.exist(staffId)
      if (!existStaff) {
        return {
          errorMessage: '员工不存在'
        }
      }
      if (!roleIds?.length) {
        return {
          errorMessage: '角色ids不能为空'
        }
      }
      await permissionModel.createStaffRole(staffId, roleIds)
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new PermissionService()
